package com.ibeeking.found.msg.rest.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ibeeking.found.msg.rest.service.ISendSysMsgService;
import com.ibeeking.found.msg.rest.service.ISysMsgTemplateService;
import com.ibeeking.found.msg.service.common.dos.SysMsgRecordDO;
import com.ibeeking.found.msg.service.common.dos.SysMsgRecordReceiverDO;
import com.ibeeking.found.msg.service.common.vo.SysMsgTemplateVO;
import com.ibeeking.found.msg.service.mapper.SysMsgRecordMapper;
import com.ibeeking.found.msg.service.mapper.SysMsgRecordReceiverMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrSubstitutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SendSysMsgServiceImpl
 * @Description 发送模板邮件
 * @Author ibeeking
 * @Date 2020-12-10 9:21
 **/
@Service
public class SendSysMsgServiceImpl implements ISendSysMsgService {
    @Resource
    private ISysMsgTemplateService sysMsgTemplateServiceImpl;

    @Resource
    private SysMsgRecordMapper sysMsgRecordMapper;

    @Resource
    private SysMsgRecordReceiverMapper sysMsgRecordReceiverMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean sendSysSimpleMsg(Long tenantId, Integer receiverType, List<Long> receiverIds, String subject, String content) {
        SysMsgRecordDO sysMsgRecordDO = new SysMsgRecordDO();
        Long recordId = IdGenerator.getId();
        sysMsgRecordDO.setId(recordId);
        sysMsgRecordDO.setTenantId(tenantId);
        sysMsgRecordDO.setReceiverType(receiverType);
        sysMsgRecordDO.setMsgSubject(subject);
        sysMsgRecordDO.setMsgContent(content);
        sysMsgRecordDO.setMsgStatus(1);
        sysMsgRecordDO.setCreateTime(LocalDateTime.now());
        if(Collections3.isNotEmpty(receiverIds)){
            List<SysMsgRecordReceiverDO> sysMsgRecordReceiverDOS = new ArrayList<>();
            receiverIds.stream().forEach(d->{
                SysMsgRecordReceiverDO recordReceiverDO = new SysMsgRecordReceiverDO();
                recordReceiverDO.setId(IdGenerator.getId());
                recordReceiverDO.setTenantId(tenantId);
                recordReceiverDO.setRecordId(recordId);
                recordReceiverDO.setReceiverType(receiverType);
                recordReceiverDO.setReceiver(d);
                recordReceiverDO.setReadStatus(0);
                recordReceiverDO.setCreateTime(LocalDateTime.now());
                sysMsgRecordReceiverDOS.add(recordReceiverDO);
            });
            if(Collections3.isNotEmpty(sysMsgRecordReceiverDOS)){
                sysMsgRecordReceiverMapper.insertBatchSomeColumn(sysMsgRecordReceiverDOS);
            }
        }
        return sysMsgRecordMapper.insert(sysMsgRecordDO)>0;
    }

    @Override
    public Boolean sendSysTemplateMsg(Long tenantId, String templateCode, Integer receiverType, List<Long> receiverIds, Map<String, String> templateData) {
        SysMsgTemplateVO sysMsgTemplateVO = sysMsgTemplateServiceImpl.viewByCode(templateCode);
        if (ObjectUtil.isNull(sysMsgTemplateVO)) {
            throw new BusinessException("系统消息模板查询失败 templateId:" + templateCode);
        }
        String templateContent = sysMsgTemplateVO.getTemplateContent();
        String templateSubject = sysMsgTemplateVO.getTemplateSubject();
        if (StringUtils.isBlank(templateContent)) {
            throw new BusinessException("系统消息发送失败 模板内容为空 templateId:" + templateCode);
        }
        // 组装模板内容 模板格式参考 "<p>您好，${name}，您的工号是${idNumber},您的年龄是${age}</p>";
        StrSubstitutor strSubstitutor = new StrSubstitutor(templateData);
        String content = strSubstitutor.replace(templateContent);
        LogUtils.info(">>>>>>>sendSysTemplateMsg message:{}", JsonUtils.toJsonStr(content));
        return sendSysSimpleMsg(tenantId, receiverType, receiverIds,  templateSubject, content);
    }
}
